/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.jjsjshop.com
 * 注意：
 * 本软件为www.jjsjshop.com开发研制，项目使用请保留此说明
 */
const app = getApp()

Component({
    properties: {
        orderInfo: {
            type: Object,
            value: {}
        },
        callPay: {
            type: Boolean,
            value: false
        },
        contact: {
            type: Boolean,
            value: false
        }
    },
    data: {
        loading: false
    },
    ready() {
        let that = this
        setTimeout(function () {
            if (that.data.callPay) {
                that.unifiedOrder()
            }
        }, 1000)
    },
    methods: {
        orderReceive() {
            let that = this
            wx.showModal({
                content: '是否确认收货吗？',
                cancelText: '我再想想',
                confirmColor: '#ff0000',
                success(res) {
                    if (res.confirm) {
                        let id = that.data.orderInfo.id
                        app.api.orderReceive(id)
                            .then(res => {
                                that.triggerEvent('orderReceive', res)
                            })
                    }
                }
            })
        },
        orderCancel() {
            let that = this
            wx.showModal({
                content: '确认取消该订单吗？',
                cancelText: '我再想想',
                confirmColor: '#ff0000',
                success(res) {
                    if (res.confirm) {
                        let id = that.data.orderInfo.id
                        app.api.orderCancel(id)
                            .then(res => {
                                that.triggerEvent('orderCancel', res)
                            })
                    }
                }
            })
        },
        orderDel() {
            let that = this
            wx.showModal({
                content: '确认删除该订单吗？',
                cancelText: '我再想想',
                confirmColor: '#ff0000',
                success(res) {
                    if (res.confirm) {
                        let id = that.data.orderInfo.id
                        app.api.orderDel(id)
                            .then(res => {
                                that.triggerEvent('orderDel', res)
                            })
                    }
                }
            })
        },
        unifiedOrder() {
            this.setData({
                loading: true
            })
            var that = this
            let orderInfo = this.data.orderInfo
            app.api.unifiedOrder({
                id: orderInfo.id
            })
                .then(res => {
                    this.setData({
                        loading: false
                    })
                    if (orderInfo.paymentPrice <= 0) {//0元付款
                        that.triggerEvent('unifiedOrder', res)
                    } else {
                        let payData = res.data;
                        if (payData != null && payData.status == "0") {
                            var pay_info = JSON.parse(payData.pay_info);
                            wx.requestPayment({
                                'timeStamp': pay_info.timeStamp,
                                'nonceStr': pay_info.nonceStr,
                                'package': pay_info.package,
                                'signType': pay_info.signType,
                                'paySign': pay_info.paySign,
                                'success': function (res) {
                                    that.triggerEvent('unifiedOrder', res)
                                },
                                'fail': function (res) {

                                },
                                'complete': function (res) {
                                    console.log(res)
                                    wx.showModal({
                                        title: '提示',
                                        content: '是否支付完成',
                                        confirmText: '完成支付',
                                        cancelText: '取消支付',
                                        success(res) {
                                            if (res.confirm) {
                                                console.log('用户点击确定')
                                                wx.navigateTo({
                                                    url: '/pages/order/order-list/index?status=4',
                                                })
                                            } else if (res.cancel) {
                                                console.log('用户点击取消')
                                            }
                                        }
                                    })
                                }
                            })
                        }
                    }
                }).catch(() => {
                this.setData({
                    loading: false
                })
            })
        },
        urgeOrder() {
            // wx.showToast({
            //     title: '已提醒卖家发货',
            //     icon: 'success',
            //     duration: 2000
            // })

            let that = this
            wx.showModal({
                title: '退款提示',
                content: '您好，亲爱的客户，退款只能线下办理，详情咨询0851-84844703',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        console.log('用户点击确定')
                        that.triggerEvent('orderDiss', res)
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        },
        orderLogistics() {
            wx.navigateTo({
                url: '/pages/order/order-logistics/index?id=' + this.data.orderInfo.orderLogistics.id
            })
        },
        orderAppraise() {
            wx.navigateTo({
                url: '/pages/appraises/form/index?orderId=' + this.data.orderInfo.id
            })
        }
    }
})
